﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DbFrame.Test
{
    using Models;
    using DbFrame;
    using DbFrame.Class;

    using System.Data;

    class Program
    {
        static void Main(string[] args)
        {
            System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch();
            s.Start();

            DBContext.Initialization("Server=.;Database=AppDB;User ID=sa;Password=123456;");

            //如果是 mysql 或者其他 数据库 则初始化不一样
            //var _MySqlExtend = new DbFrame.Test.Extend.SqlExtend();//这是一个 举例 初始化  mysql 

            DBContext db = new DBContext();
            List<SQL> li = new List<SQL>();

            var orderbyFile = "User_CreateTime";

            var topValue = 1;

            var __Sys_UserM_3 = db.Find<Sys_UserM>(select => new { SqlString = "top " + topValue + " *" },
                w => w.User_CreateTime.SqlStringCompare("CONVERT(varchar(100),@F, 23)") > DateTime.Now,
                orderby => orderbyFile + " desc");

            var __Sys_UserM_4 = db.Find<Sys_UserM>(select => new { SqlString = "top 1 *" },
                w => w.User_CreateTime.SqlStringCompare("CONVERT(varchar(100),@F, 23)") > DateTime.Now,
                orderby => new { SqlString = orderbyFile + " desc" });



            var __Sys_UserM_0_0 = db.Find<Sys_UserM>(select => new { SqlString = "top 1 *" },
               w => (w.User_Name == "Test" || w.User_Name == "Test1") && w.User_ID == Guid.Empty,
               orderby => new { desc = orderby.User_CreateTime });

            var __Sys_UserM_0 = db.Find<Sys_UserM>(select => new { SqlString = "top 1 *" },
               w => ((w.User_CreateTime.SqlString("CONVERT(varchar(100),@F, 23)>" + DateTime.Now.ToDateTimeFormat("yyyy-MM-dd")))) && w.User_ID == Guid.Empty,
               orderby => new { desc = orderby.User_CreateTime });

            var __Sys_UserM_1 = db.Find<Sys_UserM>(select => new { SqlString = "top 1 *" },
               w => w.User_CreateTime.SqlString("CONVERT(varchar(100),@F, 23)>getdate()"),
               orderby => new { desc = orderby.User_CreateTime });

            var __Sys_UserM_2 = db.Find<Sys_UserM>(select => new { SqlString = "top 1 *" },
                w => w.User_CreateTime.SqlStringCompare("CONVERT(varchar(100),@F, 23)") > DateTime.Now,
                orderby => new { desc = orderby.User_CreateTime });

            string value = "123";

            //修改
            db.UpdateCustomSet<Sys_UserM>(set => new
            {
                User_IsDelete = set.User_IsDelete + 1,
                User_Name = "23123",
                SqlString = "User_IsDelete+=" + 1,
            }, w => w.User_ID == Guid.Empty);

            var Iquery = db.Query<MemberM, Sys_UserM>((a, b) => new
            {
                SqlString_1 = " case when a.Member_Num==1 then '我是1' else '我是else' end Member_Num ",
                a.Member_Name,
                a.Member_Sex,
            })
                .Join((a, b) => a.Member_UserID == b.User_ID, "b")
                .Where((a, b) => a.Member_Sex == "男")
                .OrderBy((a, b) => new { desc = a.Member_Sex, SqlString_1 = " case when a.Member_Num==1 then '我是1' else '我是else' end Member_Num " });

            var sql = Iquery.ToSQL();

            Iquery = db.Query<MemberM, Sys_UserM>((a, b) => @" case when a.Member_Num==1 then '我是1' else '我是else' end Member_Num,
                a.Member_Name,
                a.Member_Sex, ")
                .Join((a, b) => a.Member_UserID == b.User_ID, "b")
                .Where((a, b) => a.Member_Sex == "男")
                .OrderBy((a, b) => "a.Member_Sex desc");

            var _tosql = Iquery.ToSQL();
            var _tolist = Iquery.ToList<MemberM>();
            var _tolistdic = Iquery.ToList<Dictionary<string, object>>();
            var _getpar = Iquery.GetSqlParameters();

            for (int i = 0; i < 10; i++)
            {
                var id = db.Insert<Sys_UserM>(new Sys_UserM()
                {
                    User_Email = i + "test@qq.com",
                    User_LoginName = i + "hzyNew",
                    User_Name = i + "hzyTestNew",
                    User_Pwd = i + "123"
                }, li);

                if (!db.Update<Sys_UserM>(() => new Sys_UserM
                 {
                     User_Name = "修改过" + i + "hzyTestNew"
                 }, w => w.User_ID == id.ToGuid(), li))
                {
                    Console.WriteLine("错误！");
                }
            }

            db.Delete<Sys_UserM>(w => w.User_CreateTime > DateTime.Now.AddDays(-1), li);

            if (!db.Commit(li))
            {
                Console.WriteLine("事务提交失败！");
            }

            var _Sys_UserM = new Sys_UserM();
            _Sys_UserM.User_Email = "1123@QQ.COM";
            _Sys_UserM.User_LoginName = "admin";
            _Sys_UserM.User_Name = "hzy";
            _Sys_UserM.User_Pwd = "123";

            //验证实体
            if (db.CheckModel(_Sys_UserM))
            {
                db.Insert(_Sys_UserM);
            }
            else
            {
                Console.WriteLine(db.ErrorMessge);
            }



            Console.WriteLine(" 耗时：" + (s.ElapsedMilliseconds * 0.001) + " s");
            Console.ReadKey();
        }
    }




}
